package com.example.springbootmaster.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.springbootmaster.model.User;
import com.example.springbootmaster.model.request.UserLoginRequest;
import com.example.springbootmaster.model.request.UserRegisterRequest;
import com.example.springbootmaster.service.UserService;

import java.util.ArrayList;
import java.util.Collections;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.util.List;
import java.util.stream.Collectors;

import static com.example.springbootmaster.annotation.UserConstant.USER_LOGIN_STATE;
import static org.bouncycastle.asn1.x500.style.RFC4519Style.l;
import static org.bouncycastle.asn1.x500.style.RFC4519Style.userPassword;

/**
 * @RestController 适用于编写restful api接口，返回的是json数据
 * @RequestMapping("/user") 是一个路径映射注解，用于将请求路径映射到控制器的处理方法上。
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;


    @PostMapping("/register")
    public Long userRegister(@RequestBody UserRegisterRequest userRegisterRequest){
        if(userRegisterRequest == null)
        {
            return null;
        }
        String userAccount = userRegisterRequest.getUserAccount();
        String userPassword = userRegisterRequest.getUserPassword();
        String checkPassword = userRegisterRequest.getCheckPassword();
        if(StringUtils.isAnyBlank(userAccount,userPassword,checkPassword))
        {
            return null;
        }
        return userService.userRegister(userAccount, userPassword, checkPassword);
    }
    @PostMapping("/login")
    public User doLogin(@RequestBody UserLoginRequest userLoginRequest,HttpServletRequest request){
        if(userLoginRequest == null)
        {
            return null;
        }
        String userAccount = userLoginRequest.getUserAccount();
        String userPassword = userLoginRequest.getUserPassword();
        if(StringUtils.isAnyBlank(userAccount,userPassword))
        {
            return null;
        }
        return userService.userLogin(userAccount, userPassword, request);
    }

    @GetMapping("/search")
    public List<User> searchUser(String username, HttpServletRequest request){
        if(!isAdmin(request)){
            return new ArrayList<>();
        }
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(username)){
            queryWrapper.like(true,"username",username + "%");
        }
        List<User> list = userService.list(queryWrapper);
        return list.stream().map(
                user-> userService.getSafetyUser(user)
        ).collect(Collectors.toList());
    }
    @DeleteMapping
    public Boolean deleteUser(@RequestBody Long id,HttpServletRequest request){
        if(!isAdmin(request)){
            return false;
        }
       if (id<=0){
           return false;
       }
       return userService.removeById(id);
    }

    /**
     * 是否为管理员
     * @param request
     * @return
     */
    private boolean isAdmin( HttpServletRequest request){
        //仅管理员可用
        Object attribute = request.getSession().getAttribute(USER_LOGIN_STATE);
        User safetyUser = (User) attribute;
        return safetyUser != null && StringUtils.equals(safetyUser.getUserRole(), "admin");
    }

}
